package com.service.impl;

import com.dao.GoodsDao;
import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Goods;
import com.page.PageInfo;
import com.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsDao goodsDao;
    public Goods getByGid(String gid){
      return goodsDao.getByGid(gid);
    }
    //业务逻辑层，根据业务逻辑确定返回给页面的消息和状态以及对象
    public BaseDto<Goods> getAll(){
        List<Goods> goodss = goodsDao.getAll();
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = goodss.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Goods> dto = new BaseDto<>(messageEnum,goodss);
        return dto;
    }

    public BaseDto<Goods> getAllByPage(PageInfo pageInfo){
        List<Goods> goodss = goodsDao.getAllByPage(pageInfo);
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = goodss.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Goods> dto = new BaseDto<>(messageEnum,goodss);
        dto.setObj(pageInfo);
        return dto;
    }


    public BaseDto<Goods> searchNameByPage(PageInfo pageInfo,String adname){
        List<Goods> goodss = goodsDao.searchNameByPage(pageInfo,"%"+adname+"%");
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = goodss.size() > 0?MessageEnum.SEARCHADMIN_SUCCESS:MessageEnum.SEARCHADMIN_FAIL;
        BaseDto<Goods> dto = new BaseDto<>(messageEnum,goodss);
        dto.setObj(pageInfo);
        return dto;
    }

    /**
     * 根据主键查询
     * @param gid
     * @return
     */
    public BaseDto getGoodsByGid(String gid){
        Goods goods = goodsDao.getGoodsByGid(gid);
        MessageEnum messageEnum = Optional.ofNullable(goods).isPresent()?MessageEnum.SELECT_ADMINBYPK_SUCCESS:MessageEnum.SELECT_ADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(goods);
        return dto;
    }

    /**
     * 添加一个实体
     * @param goods
     * @return
     */
    public BaseDto addGoods(Goods goods){
        int result = goodsDao.addGoods(goods);
        MessageEnum messageEnum = result>0?MessageEnum.ADDADMIN_SUCCESS:MessageEnum.ADDADMIN_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(goods);
        return dto;
    }
    /**
     * 修改实体
     */
    public BaseDto updateGoods(Goods goods){
        int result = goodsDao.updateGoods(goods);
        MessageEnum messageEnum = result>0?MessageEnum.UPDATEADMINBYPK_SUCCESS:MessageEnum.UPDATEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(goods);
        return dto;
    }
    public BaseDto updateGoodsByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param gid
     * @return
     */
    public BaseDto deleteGoodsByPrimayKey(String gid){
        int result = goodsDao.deleteGoodsByPrimayKey(gid);
        MessageEnum messageEnum = result>0?MessageEnum.DELETEADMINBYPK_SUCCESS:MessageEnum.DELETEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteGoodsByExample(){
        return null;
    }

}
